package drds.server.route.shard_algorithm;

import java.math.BigInteger;

/**
 * number column partion by Mod operator if count is 10 then 0 to 0,21 to 1 (21
 * % 10 =1)
 */
public class ByMod extends AbstractShardAlgorithm implements ShardAlgorithm {

	private int mod;

	@Override
	public void init() {

	}

	public void setMod(int mod) {
		this.mod = mod;
	}

	@Override
	public Integer calculate(String columnValue) {
		try {
			BigInteger $columnValue = new BigInteger(columnValue).abs();
			return ($columnValue.mod(BigInteger.valueOf(mod))).intValue();
		} catch (NumberFormatException e) {
			throw new IllegalArgumentException(new StringBuilder().append("columnValue:").append(columnValue).append(" Please eliminate any quote and non number within it.").toString(), e);
		}

	}

	@Override
	public int getShardNum() {
		return this.mod;
	}

	public static void main(String[] args) {
		ByMod shardByMod = new ByMod();
		shardByMod.mod = 8;
		shardByMod.calculate("\"6\"");
		shardByMod.calculate("\'6\'");
	}
}